package com.mpq.config;

import com.mpq.utils.MyException;
import com.mpq.utils.R;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * @author mingpingqiang on 2018/11/29/16:01
 * Created by mingpingqiang on 2018/11/29/16:01
 */
@ControllerAdvice
public class GlobalExceptionHander {
    @ResponseBody
    @ExceptionHandler(value = MyException.class)
    public R myExceptionHander(MyException ex) throws Exception{
        return R.error(ex.getCode(),ex.getMsg());
    }

    @ResponseBody
    @ExceptionHandler(value = BindException.class)
    public R myExceptionHander(BindException ex) throws Exception{
        StringBuilder sb = new StringBuilder();
        FieldError fieldError = ex.getFieldError();
        sb.append(fieldError.getDefaultMessage());
        return R.error(500,sb.toString());
    }
}
